package com.example.permissionservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.permissionservice.common.aspect.CheckUserStatus;
import com.example.permissionservice.common.enums.ResultCode;
import com.example.permissionservice.common.enums.RoleEnum;
import com.example.permissionservice.common.utils.ThreadLocalUtil;
import com.example.permissionservice.mapper.RoleMapper;
import com.example.permissionservice.mapper.UserRoleMapper;
import com.example.permissionservice.model.Result;
import com.example.permissionservice.model.Role;
import com.example.permissionservice.model.UserRole;
import com.example.permissionservice.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/6/15
 */
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Result<Void> bindDefaultRole(Long userId) {
        UserRole existUserRole = userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserId, userId));
        if (existUserRole != null) {
            log.warn("用户 {} 已有角色，跳过绑定", userId);
        }
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(RoleEnum.USER.getRoleId());
        userRoleMapper.insert(userRole);
        return Result.ok();
    }

    @Override
    public Result<String> getUserRoleCode(Long userId) {
        UserRole userRole = userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserId, userId));
        if (userRole == null) {
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        } else if (userRole.getUserId() == null) {
            return Result.fail(ResultCode.USER_EXCEPTION);
        } else {
            Role role = roleMapper.selectOne(new LambdaQueryWrapper<Role>()
                    .eq(Role::getRoleId, userRole.getRoleId()));
            return Result.ok(role.getRoleCode());
        }
    }

    @Override
    @CheckUserStatus
    public Result<Void> upgradeToAdmin(Long userId) {
        UserRole userRole = userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserId, userId));
        if(userRole == null){
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        userRole.setRoleId(RoleEnum.ADMIN.getRoleId());
        userRoleMapper.updateById(userRole);
        return Result.ok();
    }

    @Override
    @CheckUserStatus
    public Result<Void> downgradeToUser(Long userId) {
        ThreadLocalUtil.set("userId",userId);
        UserRole userRole = userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserId, userId));
        if(userRole == null){
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        userRole.setRoleId(RoleEnum.USER.getRoleId());
        userRoleMapper.updateById(userRole);
        return Result.ok();
    }
}
